package com.xiaoshuidi.cloud.module.contract.dal.mysql.bill;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.mybatis.core.mapper.BaseMapperX;
import com.xiaoshuidi.cloud.module.contract.controller.admin.bill.vo.OrderDetaillAndOrderFeeVo;
import com.xiaoshuidi.cloud.module.contract.controller.admin.bill.vo.OrderItemInfo;
import com.xiaoshuidi.cloud.module.contract.controller.admin.bill.vo.ownerOrder.*;
import com.xiaoshuidi.cloud.module.contract.controller.admin.bill.vo.rentOrder.RentOrderCountVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.bill.vo.rentOrder.RentOrderQueryVo;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.HomeQueryVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.WorkbenchTotalResp;
import com.xiaoshuidi.cloud.module.contract.controller.app.invoice.vo.InvoiceRespVO;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.bill.OrderFeesDO;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.bill.OwnerOrderTransferDO;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.bill.RentOrderDO;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.bill.RentOrderEXDO;
import com.xiaoshuidi.cloud.module.contract.service.bill.dto.RentOrderQueryDTO;
import com.xiaoshuidi.cloud.module.contract.service.home.dto.HomeDataDTO;
import com.xiaoshuidi.cloud.module.contract.vo.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.poi.ss.formula.functions.T;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 账单 Mapper
 *
 * @author 合房租赁
 */
@Mapper
public interface RentOrderMapper extends BaseMapperX<RentOrderDO> {

    List<ContractInvoiceDetailVO> getOrderDetailList(@Param("invoiceId") Long invoiceId);

    List<ContractInvoiceDetailVO> getOrderDetailListByOrderNo(@Param("invoiceId") Long invoiceId);

    IPage<OrderInvoicePageRespVO> getInvoiceList(Page<OrderInvoicePageRespVO> page, @Param("pageReqVO") OrderInvoicePageReqVO pageReqVO);


    IPage<OrderItemInfo> getReceivableBills(IPage<T> mpPage, @Param("billDate") LocalDate billDate,@Param("apartmentId") Long apartmentId);

    IPage<RentOrderEXDO> getRentOrder(IPage page, @Param("rentOrderQueryVo") RentOrderQueryVo rentOrderQueryVo, @Param("tenantId") Long tenantId);
    List<RentOrderEXDO> getRentOrder( @Param("rentOrderQueryVo") RentOrderQueryVo rentOrderQueryVo, @Param("tenantId") Long tenantId);

    IPage<OwnerOrderRespVO> getOwnerOrderList(Page<OwnerOrderRespVO> page, @Param("param") OwnerOrderPageReqVO param);

    List<OwnerOrderRespVO> getOwnerOrderList(@Param("param") OwnerOrderPageReqVO param);

    List<OrderDetaillAndOrderFeeVo> getOrder(@Param("loginUserId") Long loginUserId, @Param("payStatus") String payStatus);

    RentOrderCountVO getContractOrderCount(@Param("contractId") Long contractId);

    List<RentOrderDO> getOrderListByCompanyContractId(@Param("query") RentOrderQueryDTO queryDTO);

    /**
     * 查询发票账单
     */
    List<OrderDetaillAndOrderFeeVo> getInvoiceOrder(@Param("loginUserId") Long loginUserId, @Param("invoiceStatus") Integer invoiceStatus);

    int updatePayStatusCancel(@Param("contractId") Long contractId);

    int updateMemberIdByContractIdUnPaid(@Param("contractId") Long contractId, @Param("memberId") Long memberId);


    /**
     * 时间区间统计应付账单
     * @param startDate
     * @param endDate
     * @return: java.lang.Integer
     */
    Integer countPayableOrder(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    IPage<OwnerOrderPageRespVO> getOwnerOrderHomePage(Page<OwnerOrderPageRespVO> page, @Param("param") HomeQueryVO param);

    List<HomeDataDTO> getHomeData(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate,@Param("apartmentId") Long apartmentId);
    List<HomeDataDTO> getHomeMonthData(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate,@Param("apartmentId") Long apartmentId);

    List<HomeDataDTO> getHomeMonthBill(@Param("receivableDate") LocalDate receivableDate, @Param("endDate") LocalDate endDate,@Param("apartmentId") Long apartmentId);

    List<HomeDataDTO> getHomeMonthContract(@Param("receivableDate") LocalDate receivableDate, @Param("endDate") LocalDate endDate,@Param("apartmentId") Long apartmentId);

    IPage<CompanyInvoiceRespVO> selectCompanyInvoiceable(CompanyInvoiceReqVO reqVO, IPage<CompanyInvoiceRespVO> page);

    List<CompanyInvoiceRespVO> selectCompanyInvoiceableDetail(@Param("reqVO") CompanyInvoiceReqVO reqVO);

    IPage<InvoiceRespVO> selectInvoiceable(Long memberId, Page<InvoiceRespVO> page);

    WorkbenchTotalResp selectTodayTotalFeeAmount(@Param("apartmentId") Long apartmentId);

    BigDecimal selectNotReceivedTotal(@Param("apartmentId") Long apartmentId);

    int createRenterOrderTransfer();

    int createRenterOrderTransferByOrderIds(@Param("ids") List<Long> ids);

    int countContractOwnerBankTransfer(@Param("id") Long id);

    IPage<RentOrderDO> selectOwnerPendingApprovalPage(IPage<OwnerOrderTransferDO> mpPage);

    IPage<OwnerOrderTransferDO> selectOwnerTransferPage(@Param("example") OwnerOrderTransferPageReqVO reqVO,
                                                        IPage<OwnerOrderTransferDO> mpPage);

    List<OwnerOrderTransferDO> selectOwnerTransfer(Object[] id);

    int confirmOwnerTransfer(Collection<Long> id, String status, String operator);

    List<Map<String, Object>> selectOwnerTransferConfirmSummary(@Param("example") OwnerOrderTransferPageReqVO reqVO);

    IPage<RentOrderDO> selectReminderOrder(IPage<T> mpPage);

    int updateOwnerOrderTransfer(OwnerOrderTransferCallbackReqVO reqVO);

    void updateMemberUser(@Param("mobile")String mobile, @Param("userName") String userName, @Param("userId") Long userId,@Param("oldUserId")  Long oldUserId);

    int updateOwnerOrder(OwnerOrderTransferCallbackReqVO reqVO);

    RentOrderDO selectPaymentOrder(@Param("id") Long id);

    List<OrderFeesDO> getNumOneFeesByContractId(@Param("contractId") Long contractId);

    @Select({"select o.* " +
            "from contract_rentorder o " +
            "where o.suspend = false " +
            "and o.pay_status in ('payment') " +
            "and o.deleted = '0' " +
            "and o.actual_pay_time between #{startTime} and #{endTime}"})
    List<RentOrderDO> getPaymentRentOrderList(@Param("startTime")String startTime,@Param("endTime")String endTime);
}
